var flashphoner;
var micVolume = 100;
var speakerVolume = 100;
var registerRequired;

var callee = '';
function call(){
	flashphoner.loginClick2Call();
	toHangupState();
}
function hangup(){
	flashphoner.hangup();
	getElement('callButton').style.backgroundColor = "gray";
	getElement('callButton').disabled="disabled";
}
function logoff(){
	flashphoner.logoff();
}

//notified function
function notifyRegisterRequired(registerR){
	registerRequired = registerR;
}
function notifyCloseConnection(){
	toCallState();
}

function notifyConnected(){
	if (!registerRequired){
		flashphoner.call(null,'Caller');
	}	
}
function notifyRegistered(){
	if (registerRequired){
		flashphoner.call(null,'Caller');
	}
}
function notifyBalance(balance){
}
function notifyCost(cost){
}

function notify(state){
	if (state == STATE_BUSY || state == STATE_FINISH){
		toCallState();
		logoff();
	}	
}
function notifyCall(caller,visibleNameCaller){
	hangup();
}

function notifyError(error){
	if (error == CONNECTION_ERROR){	
		openInfoView("Connection fail",3000);
	} else if (error == AUTHENTICATION_FAIL){				
		openInfoView("Register fail",3000);
		window.setTimeout("logoff();", 3000);
	} else if (error == USER_NOT_AVAILABLE){
		openInfoView("Callee not found!",3000);
	} else if (error == TOO_MANY_REGISTER_ATTEMPTS){
		openInfoView("Connection error",3000);
		toLoggedOffState();
	} else if (error == LICENSE_RESTRICTION){
		openInfoView("License restriction",3000);
	} else if (error == INTERNAL_SIP_ERROR){
		openInfoView("Unknown error",3000);
	} else if (error == REGISTER_EXPIRE){
		openInfoView("Check SIP account settings",3000);
	} else if (error == SIP_PORTS_BUSY){
		openInfoView("All sip ports is busy",3000);
	} else if (error == MEDIA_PORTS_BUSY){
		openInfoView("All media ports is busy",3000);
	} else if (error == WRONG_SIPPROVIDER_ADDRESS){
		openInfoView("Wrong sip provider address",3000);
	}				
	toCallState();	
	logoff();
} 

//additional functions
function toHangupState(){
	getElement('callButton').value = "Hangup";
	disableCallButton();
}

function toCallState(){
	getElement('callButton').value = "Call";
	disableCallButton();
}
function disableCallButton(){
	window.setTimeout("getElement('callButton').disabled='';	getElement('callButton').style.backgroundColor = '#009800';", 3000);
	getElement('callButton').style.backgroundColor = "gray";
	getElement('callButton').disabled="disabled";
}

function openInfoView(str,timeout){
	if (timeout != 0){
		window.setTimeout("closeInfoView();", timeout);
	}
	getElement('infoDiv').style.visibility = "visible";
	getElement('infoText').innerHTML = str;
}
function closeInfoView(){
	getElement('infoDiv').style.visibility = "hidden";
}

function openSettingsView(){
	getElement('settingsDiv').style.visibility = "visible";
}
function closeSettingsView(){
	getElement('settingsDiv').style.visibility = "hidden";
}
function saveMicSettings(){
	flashphoner.setVolume(speakerVolume);
    flashphoner.setMicVolume(micVolume);
    closeSettingsView();
}

function getElement(str){
	return document.getElementById(str);	
}
